package com.dundun.SSO.core.impl;


import com.dundun.SSO.core.BaseService;
import com.dundun.SSO.core.dao.BaseDao;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;

public abstract class BaseServiceImpl<T> implements BaseService<T> {
    private Class<T> entityClass;

    @Autowired
    protected BaseDao<T> dao;

    public BaseServiceImpl(){
        Type genType = this.getClass().getGenericSuperclass();
        Type[] params = ((ParameterizedType)genType).getActualTypeArguments();
        this.entityClass = (Class)params[0];
    }

    public BaseDao<T> getDao(){
        return this.dao;
    }

    public T selectByPrimaryKey(Object key) {
        return this.dao.selectByPrimaryKey(key);
    }

    public T selectOne(T example) {
        return this.dao.selectOne(example);
    }

    public List<T> selectByExample(Object example) {
        return this.dao.selectByExample(example);
    }

    public List<T> selectAll() {
        return this.dao.selectAll();
    }

    public int save(T entity) {
        return this.dao.insert(entity);
    }

    public int saveNotNull(T entity) {
        return this.dao.insertSelective(entity);
    }

    public int delete(Object key) {
        return this.dao.deleteByPrimaryKey(key);
    }

    public int updateByPrimaryKey(T entity) {
        return this.dao.updateByPrimaryKey(entity);
    }

    public int updateNotNull(T entity) {
        return this.dao.updateByPrimaryKeySelective(entity);
    }

    public int selectCountByExample(Object example){return this.dao.selectCountByExample(example);}

    public int insertList(List<T> list){return this.dao.insertList(list);}

    public int selectCount(T entity){return this.dao.selectCount(entity);}

}
